package sisi.login;

import java.util.Map;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Row;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;
/**
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0
 * 
 */
@SuppressWarnings("serial")
public class LoginWindow extends Window implements
		org.zkoss.zk.ui.ext.AfterCompose {
	private Window loginWindow;
	private Textbox utente, password, password2, email;
	private Div errorlogin, errorcambiopassword, errorinviopassword;
	private Row utenterow, passwordrow, nuovapasswordrow, emailrow;
	private Button btnLogin, btnModificapassword, btnInviapasswordpermail;

	protected Map<String, Object> args;

	public void onCreate$loginWindow(Event event) {
		loginWindow.setTitle("Login");
		utente.setFocus(true);
		errorlogin.setVisible(false);
		errorcambiopassword.setVisible(false);
		errorinviopassword.setVisible(false);
	}

	public void afterCompose() {
		// wire variables
		ConventionWires.wireVariables(this, this);
		// auto forward
		ConventionWires.addForwards(this, this);
	}

	public void onClick$btnLogin(){
		controlla();
	}
	
	public void onClick$btnModificapassword(){
		nuovapasswordrow.setVisible(!nuovapasswordrow.isVisible());
		if (nuovapasswordrow.isVisible()){
			Clients.showNotification("Indicare utente, password e nuova password", "info", null, "middle_center", 2000);
			btnLogin.setLabel("Conferma Modifica Password");
			btnModificapassword.setLabel("Ritorna al Login");
			btnInviapasswordpermail.setVisible(false);
		} else {
			btnLogin.setLabel("Login");
			btnModificapassword.setLabel("Modifica Password");
			btnInviapasswordpermail.setVisible(true);
		}
		errorlogin.setVisible(false);
		errorcambiopassword.setVisible(false);
		errorinviopassword.setVisible(false);
	}
	
	public void onOK$utente(){
		password.setFocus(true);
	}
	
	public void onOK$password(){
		if (nuovapasswordrow.isVisible()){
			password2.setFocus(true);
		} else {
			controlla();
		}
	}
	
	public void onOK$password2(){
		controlla();
	}
	
	public void onOK$email(){
		controlla();
	}
	
	private void controlla(){
		
		if (nuovapasswordrow.isVisible()){ // cambia password
			Integer verifica = new LoginController().LoginCambiaPassword(utente.getValue(), password.getValue(), password2.getValue());
			if (verifica==0){
				Clients.showNotification("Password modificata correttamente", "info", null, "middle_center", 2000);
				loginWindow.detach();
			} else {
				errorcambiopassword.setVisible(true);
			}
		} else if ( emailrow.isVisible() ){
			Integer verifica = new LoginController().LoginInviaPassword(email.getValue());
			if (verifica==0){
				Clients.showNotification("Password inviata correttamente", "info", null, "middle_center", 2000);
				clickPasswordMail();
			} else {
				errorinviopassword.setVisible(true);
			}
			
		} else { // login
		
			Integer verifica = new LoginController().LoginVerifica(utente.getValue(), password.getValue());
			if (verifica==0){
				errorlogin.setVisible(false);
				loginWindow.detach();
			} else {
				errorlogin.setVisible(true);
			}
		}

	}
	
	public void onClick$btnInviapasswordpermail(){

		clickPasswordMail();
	}
	
	private void clickPasswordMail(){
		emailrow.setVisible(!emailrow.isVisible());


		if (emailrow.isVisible()){
			Clients.showNotification("Indicare indirizzo email dove inviare le credenziali..", "info", null, "middle_center", 2000);
			btnLogin.setLabel("Conferma Invio password per email");
			btnInviapasswordpermail.setLabel("Ritorna al Login");
			btnModificapassword.setVisible(false);
			utenterow.setVisible(false);
			passwordrow.setVisible(false);
		} else {
			btnLogin.setLabel("Login");
			btnInviapasswordpermail.setLabel("Invia password per email");
			btnModificapassword.setVisible(true);
			utenterow.setVisible(true);
			passwordrow.setVisible(true);
		}

		errorlogin.setVisible(false);
		errorcambiopassword.setVisible(false);
		errorinviopassword.setVisible(false);
		
	}
	
}
